package com.industrial.premu.controller;


import com.industrial.common.core.web.controller.BaseController;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.security.annotation.RequiresPermissions;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.premu.domain.PreMuBenefitEvaluation;
import com.industrial.premu.domain.SysArea;
import com.industrial.premu.dto.PreMuAreaTreeDto;
import com.industrial.premu.dto.PreMuChartsDto;
import com.industrial.premu.mapper.SysAreaMapper;
import com.industrial.premu.service.IPreMuBenefitEvaluationService;
import com.industrial.premu.util.PageUtils;
import com.industrial.premu.vo.PreMuChartsVo;
import com.industrial.premu.vo.PreMuProgressAnalysisVo;
import com.industrial.premu.vo.PreMuScheduleManagementVo;
import com.industrial.system.api.model.LoginUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 进度分析（图表）
 */
@RestController
@RequestMapping("/progressAnalysis")
public class PreMuProgressAnalysisController extends BaseController {

    @Autowired
    private IPreMuBenefitEvaluationService preMuBenefitEvaluationService;

    @Autowired
    private  SysAreaMapper sysAreaMapper;


    @GetMapping(value = "/getAreaTree")
    public AjaxResult getAreaTree(PreMuAreaTreeDto areaTreeDto) {

        LoginUser loginUser = SecurityUtils.getLoginUser();
        Set<String> roles= loginUser.getRoles();
        if(roles.contains("admin")&&StringUtils.isBlank(areaTreeDto.getAreaLeavel())){
            areaTreeDto.setAreaLeavel("1");
        }
        List<PreMuScheduleManagementVo> list=new ArrayList<>();
        int pjNum=0;
        int noPjNum=0;
        int countyCount=0;
        if(areaTreeDto.getCodes()!=null){
            LocalDate localDate = LocalDate.now();
            String years=String.valueOf(localDate.getYear() - 1);
            for (int i = 0; i < areaTreeDto.getCodes().length; i++) {
                int noData=0;
                int finashed=0;
                PreMuBenefitEvaluation preMuBenefitEvaluation =new PreMuBenefitEvaluation();
                if("1".contains(areaTreeDto.getAreaLeavel())){
                    preMuBenefitEvaluation.setCity(areaTreeDto.getCodes()[i]);
                    preMuBenefitEvaluation.setYears(years);
                    preMuBenefitEvaluation.setCompanyType(areaTreeDto.getCompanyType());
                    PreMuScheduleManagementVo preMuScheduleManagementVo= preMuBenefitEvaluationService.groupArea(preMuBenefitEvaluation);


                    PreMuChartsDto preMuChartsDto =new PreMuChartsDto();
                    preMuChartsDto.setCity(areaTreeDto.getCodes()[i]);
                    preMuChartsDto.setYears(years);
                    preMuChartsDto.setCompanyType(areaTreeDto.getCompanyType());
                    finashed=preMuBenefitEvaluationService.groupCounty(preMuChartsDto).size();
                    List<SysArea> cityList=  sysAreaMapper.selectAreaByParentId(areaTreeDto.getCodes()[i]);
                    countyCount=cityList.size();
                    noData=countyCount-finashed;
                    preMuScheduleManagementVo.setNoData(String.valueOf(noData));
                    preMuScheduleManagementVo.setFinashed(String.valueOf(finashed));
                    preMuScheduleManagementVo.setCountyCount(String.valueOf(countyCount));
                    if(countyCount-noData>0){
                        preMuScheduleManagementVo.setFlag("2");
                    }else if(countyCount-finashed==0&&finashed!=0){
                        preMuScheduleManagementVo.setFlag("1");
                    }else  if(countyCount-noData==0){
                        preMuScheduleManagementVo.setFlag("0");
                    }
                    list.add(preMuScheduleManagementVo);
            }else if("2".contains(areaTreeDto.getAreaLeavel())){
                    preMuBenefitEvaluation.setCounty(areaTreeDto.getCodes()[i]);
                    preMuBenefitEvaluation.setYears(years);
                    preMuBenefitEvaluation.setCompanyType(areaTreeDto.getCompanyType());
                    PreMuScheduleManagementVo preMuScheduleManagementVo= preMuBenefitEvaluationService.groupArea(preMuBenefitEvaluation);
                    if(Integer.valueOf(preMuScheduleManagementVo.getDaoruSum())>0){
                        if(Integer.valueOf(preMuScheduleManagementVo.getDaoruSum())-
                                Integer.valueOf(preMuScheduleManagementVo.getShangbaoSum())>0){
                            preMuScheduleManagementVo.setFlag("2");

                        }else if(Integer.valueOf(preMuScheduleManagementVo.getDaoruSum())-
                                Integer.valueOf(preMuScheduleManagementVo.getShangbaoSum())==0){
                            preMuScheduleManagementVo.setFlag("1");

                        }
                        preMuScheduleManagementVo.setFinashed(preMuScheduleManagementVo.getShangbaoSum());
                    }else{
                        preMuScheduleManagementVo.setFlag("0");
                        preMuScheduleManagementVo.setNoData("0");
                        preMuScheduleManagementVo.setFinashed("0");
                    }
                    list.add(preMuScheduleManagementVo);
                }else if("3".contains(areaTreeDto.getAreaLeavel())){
                    preMuBenefitEvaluation.setVillage(areaTreeDto.getCodes()[i]);
                    preMuBenefitEvaluation.setYears(years);
                    preMuBenefitEvaluation.setCompanyType(areaTreeDto.getCompanyType());
                    PreMuScheduleManagementVo preMuScheduleManagementVo= preMuBenefitEvaluationService.groupArea(preMuBenefitEvaluation);
                    if(Integer.valueOf(preMuScheduleManagementVo.getDaoruSum())>0){
                        if(Integer.valueOf(preMuScheduleManagementVo.getDaoruSum())-
                                Integer.valueOf(preMuScheduleManagementVo.getShangbaoSum())>0){
                            preMuScheduleManagementVo.setFlag("2");

                        }else if(Integer.valueOf(preMuScheduleManagementVo.getDaoruSum())-
                                Integer.valueOf(preMuScheduleManagementVo.getShangbaoSum())==0){
                            preMuScheduleManagementVo.setFlag("1");

                        }
                        preMuScheduleManagementVo.setFinashed(preMuScheduleManagementVo.getShangbaoSum());
                    }else{
                        preMuScheduleManagementVo.setFlag("0");
                        preMuScheduleManagementVo.setNoData("0");
                        preMuScheduleManagementVo.setFlag("0");
                    }
                    list.add(preMuScheduleManagementVo);
                }

        }
    }
        return success(PageUtils.paginateList(list,areaTreeDto.getPageNum(),areaTreeDto.getPageSize()));
    }

    @GetMapping(value = "/getPingjiCount")
    public AjaxResult getPingjiCount(PreMuAreaTreeDto areaTreeDto) {

        LoginUser loginUser = SecurityUtils.getLoginUser();
        Set<String> roles= loginUser.getRoles();
        if(roles.contains("admin")&&StringUtils.isBlank(areaTreeDto.getAreaLeavel())){
            areaTreeDto.setAreaLeavel("1");
            areaTreeDto.setProvince("370000");
        }

        LocalDate localDate = LocalDate.now();
        String years=String.valueOf(localDate.getYear() - 1);
        areaTreeDto.setYears(years);
        PreMuProgressAnalysisVo preMuProgressAnalysisVo=new PreMuProgressAnalysisVo();

        if("1".contains(areaTreeDto.getAreaLeavel())){
            List<PreMuChartsVo> preMuChartsVoList=   preMuBenefitEvaluationService.getPj(areaTreeDto);
            List<SysArea> cityList=  sysAreaMapper.selectAreaByParentId(areaTreeDto.getProvince());
            preMuProgressAnalysisVo.setPjNum(String.valueOf(preMuChartsVoList.size()));
            preMuProgressAnalysisVo.setNoPjNum(String.valueOf(cityList.size()-preMuChartsVoList.size()));
        }else if("2".contains(areaTreeDto.getAreaLeavel())){
            List<PreMuChartsVo> preMuChartsVoList=   preMuBenefitEvaluationService.getPj(areaTreeDto);
            List<SysArea> countyList=  sysAreaMapper.selectAreaByParentId(areaTreeDto.getCity());
            preMuProgressAnalysisVo.setPjNum(String.valueOf(preMuChartsVoList.size()));
            preMuProgressAnalysisVo.setNoPjNum(String.valueOf(countyList.size()-preMuChartsVoList.size()));
        }else if("3".contains(areaTreeDto.getAreaLeavel())){
       /*     PreMuBenefitEvaluation preMuBenefitEvaluation =new PreMuBenefitEvaluation();
            preMuBenefitEvaluation.setCounty(areaTreeDto.getCounty());
            preMuProgressAnalysisVo= preMuBenefitEvaluationService.getCountyPingJ(preMuBenefitEvaluation);*/
            List<PreMuChartsVo> preMuChartsVoList=   preMuBenefitEvaluationService.getPj(areaTreeDto);
            List<SysArea> villageList=  sysAreaMapper.selectAreaByParentId(areaTreeDto.getCounty());
            preMuProgressAnalysisVo.setPjNum(String.valueOf(preMuChartsVoList.size()));
            preMuProgressAnalysisVo.setNoPjNum(String.valueOf(villageList.size()-preMuChartsVoList.size()));
        }

        return  success(preMuProgressAnalysisVo);
    }

}
